<template>
  <view class="content">
    <view class="topbox">
      <view>
        <u-icon @tap="goback" color="#000000" size="40" name="arrow-left"></u-icon>
      </view>
      <view class="topRight"></view>
    </view>
    <view class="payon" v-show="paypop == 1">
      <view style="display: flex; align-items:center;">
        <image src="../static/image/pagesDelivery/await.png" mode="scaleToFill" />
        <view>待支付</view>
      </view>
      <!-- <view>
        <view style="font-weight: 400;font-size: 28rpx;color: #D12C25;margin-right:8rpx;"><u-count-down font-size="24"
            bg-color="#e6e6e6" :timestamp="timestamp"></u-count-down></view>
        <image style="width: 24rpx;height:24rpx;" src="../static/image/pagesDelivery/prompt.png" mode="scaleToFill" />
      </view> -->
    </view>
    <view class="payon" v-show="paypop == 12">
      <view style="display: flex; align-items:center;">
        <image src="../static/image/pagesDelivery/timeOut.png" mode="scaleToFill" />
        <view>订单已超时</view>
      </view>

      <view>
        <text style="font-weight: 400;font-size: 28rpx;color: #D12C25;margin-right:8rpx;">订单超时，已自动退款</text>
        <image style="width: 24rpx;height:24rpx;" src="../static/image/pagesDelivery/prompt.png" mode="scaleToFill" />
      </view>
    </view>
    <view class="payon" v-show="paypop == 8">
      <view style="display: flex; align-items:center;">
        <image src="../static/image/pagesHealth/end.png" mode="scaleToFill" />
        <view>订单已取消</view>
      </view>
      <view>
        <text style="font-weight: 400;font-size: 28rpx;color: #D12C25;margin-right:8rpx;">{{ cancelorders
          }}</text>
        <image style="width: 24rpx;height:24rpx;" src="../static/image/pagesDelivery/prompt.png" mode="scaleToFill" />
      </view>
    </view>
    <view class="payon" v-show="paypop == 9">
      <view style="display: flex; align-items:center;">
        <image src="../static/image/pagesHealth/end.png" mode="scaleToFill" />
        <view>已核销</view>
      </view>
    </view>
    <view class="payon" v-show="paypop == 2">
      <view style="display: flex; align-items:center;">
        <image src="../static/image/pagesHealth/await.png" mode="scaleToFill" />
        <view>待核销</view>
      </view>
      <!-- <view>
        <text style="font-weight: 400;font-size: 28rpx;color: #D12C25;margin-right:8rpx;">144:20:11</text>
        <image style="width: 24rpx;height:24rpx;" src="../static/image/pagesDelivery/prompt.png" mode="scaleToFill" />
      </view> -->
    </view>
    <!-- <view class="payon" v-show="paypop == 13">
      <view style="display: flex; align-items:center;">
        <image style="width: 64rpx;" src="../static/image/pagesDelivery/refund.png" mode="scaleToFill" />
        <view>退款中</view>
      </view>
      <view>
        <text style="font-weight: 400;font-size: 28rpx;color: #D12C25;margin-right:8rpx;">144:20:11</text>
        <image style="width: 24rpx;height:24rpx;" src="../static/image/pagesDelivery/prompt.png" mode="scaleToFill" />
      </view>
    </view>
    <view style="display: flex;align-items:flex-end;" v-show="paypop == 16">
      <view class="payontwo">
        <image src="../static/image/pagesDelivery/refund.png" mode="scaleToFill" />
        <view>已退款</view>
      </view>
      <view style="font-size:20rpx;color:#d12c25; margin-left:24rpx;">请到您支付平台查看退款金额</view>
    </view>
    <view class="payon" v-show="paypop == 15">
      <view style="display: flex; align-items:center;">
        <image src="../static/image/pagesDelivery/refundEnd.png" mode="scaleToFill" />
        <view>拒绝退款</view>
      </view>

    </view> -->
    <view class="ordersmain">
      <view class="ordermainone">
        <view class="orderleft">{{ orderslist.merchantName }}</view>
        <!-- <view v-show="paypop == 2" @tap="moneyback" class="orderright">申请退款</view> -->
        <view v-show="paypop == 1" @tap="showorders = true" class="orderright">取消订单</view>
      </view>
      <view class="ordermaintwo" v-for="(item, index) in orderslist.orderProductsList" :key="index">
        <view>
          <image :src="item.productImg" mode="scaleToFill" />
        </view>
        <view style="flex: 1;">
          <view class="ordertwoname">{{ item.productName }}</view>
          <view class="ordertwonum">
            <text class="ordertwotop">费用</text>
            <view>
              <text class="ordertwobottom">￥</text>
              <text class="orderbottomtwo">{{ item.productTotalPrice / 100 }}</text>
            </view>
          </view>
        </view>
      </view>

      <view class="pay">
        <view>支付方式</view>
        <view style="display:flex; align-items: center;">
          <view>
            <image src="../static/image/pagesDelivery/alipay.png" mode="scaleToFill" />
          </view>
          <view style="font-weight: 600; color: #424141;">支付宝</view>
        </view>
      </view>

    </view>
    <view class="ordersnum">
      <view class="ordersone">
        <view>订单编号</view>
        <view>
          {{ orderslist.orderNum }}
          <text @tap="paste(orderslist.orderNum)" style="padding:6rpx 12rpx; background-color:#F3F3F3; color:#333333; margin-left:24rpx;">复制</text>
        </view>
      </view>
      <view class="ordersone">
        <view>下单时间</view>
        <view>{{ orderslist.createTime }}</view>
      </view>
      <view v-show="paypop != 1 && paypop != 8" class="ordersone">
        <view>支付时间</view>
        <view>{{ orderslist.payTime }}</view>
      </view>
      <!-- <view v-show="paypop == 13 || paypop == 16 || paypop == 15" class="ordersone">
        <view>申请退款时间</view>
        <view>2024-6-13</view>
      </view>
      <view v-show="paypop == 16 || paypop == 15" class="ordersone">
        <view>退款完成时间</view>
        <view>2024-6-13</view>
      </view> -->
      <view v-show="paypop == 9" class="ordersone">
        <view>完成时间</view>
        <view>{{ orderslist.completionTime }}</view>
      </view>
      <view class="ordersone">
        <view>订单金额</view>
        <view style="color: #D12C25;">￥{{ orderslist.payMoney / 100 }}</view>
      </view>

      <!-- <view v-show="paypop == 13 || paypop == 16 || paypop == 15" class="ordersone">
        <view>退款金额</view>
        <view style="color: #D12C25;">￥{{ orderslist.payMoney / 100 }}</view>
      </view>
      <view v-show="paypop == 13" class="ordersone">
        <view>退款理由：{{ orderslist.refReason }}</view>
      </view>
      <view v-show="paypop == 15" class="ordersone">
        <view>拒绝理由：{{ orderslist.refusalOfRefund }}</view>
      </view> -->

    </view>

    <!-- 支付二维码 -->
    <view class="QrCode" v-show="paypop == 2">
      <view class="QrCodeTop">
        <view class="">
          核销码
        </view>
        <view class="Green">
          待使用
        </view>
      </view>
      <view class="QrImg">
        <image class="" :src="orderslist.orderProductsList[0].qrUrl">
        </image>
      </view>
      <view class="QrCodeM">
        <text class="">
          {{ orderslist.orderProductsList[0].qrCode }}
        </text>
        <view @tap="paste(orderslist.orderProductsList[0].qrCode)" class="">复制</view>
      </view>
      <view class="QrText">
        <view class="">
          为保障您的权益
        </view>
        <view class="">
          未到店消费前请不要将核销码提供给商家
        </view>
      </view>
    </view>

    <view class="QrCode" v-show="paypop == 9 || paypop == 16">
      <view class="QrCodeTop">
        <view class="">
          核销码
        </view>
        <view class="grey">
          已使用
        </view>
      </view>
      <view class="QrgreyImg">
        <image class="" :src="orderslist.orderProductsList[0].qrUrl"></image>
      </view>
      <view class="QrCodeM">
        <text class="">
          {{ orderslist.orderProductsList[0].qrCode }}
        </text>
        <view @tap="paste(orderslist.orderProductsList[0].qrCode)" class="">复制</view>
      </view>
      <view class="QrText">
        <view class="">
          为保障您的权益
        </view>
        <view class="">
          未到店消费前请不要将核销码提供给商家
        </view>
      </view>
    </view>
    <view v-show="paypop == 13" class="commentback">
      <text>撤销退款</text>
    </view>
    <view>
      <u-popup v-model="showorders" mode="bottom">
        <view style="padding:32rpx 32rpx 224rpx 32rpx;">
          <view style="font-weight: bold;font-size: 28rpx;color: #000000;">
            订单取消
          </view>
          <view class="radio">
            请选择取消订单原因
          </view>
          <view class="">
            <u-radio-group :wrap="true" v-model="valuerradio" @change="radioGroupChange">
              <u-radio class="radiobutton" active-color="red" @change="radioChange" v-for="(item, index) in list"
                :key="index" :name="item.name" :disabled="item.disabled">
                {{ item.name }}
              </u-radio>
            </u-radio-group>
          </view>
        </view>
        <view @tap="setcancelorder" class="gotoradio">
          <text>确认</text>
        </view>
      </u-popup>
    </view>
    <u-popup v-model="show" mode="bottom">
      <view style="padding: 32rpx 32rpx 184rpx 32rpx;">
        申请退款
        <u-form :model="form" ref="uForm">
          <u-form-item :border-bottom="false" label="" prop="name">
            <view style="display:flex;justify-content:space-between;border:1rpx solid #ccc; width:100%;padding:0 24rpx;"
              @tap="openReason">
              <view>
                <picker :value="selectedIndex" @change="onPickerChange" :range="options">
                  <view>{{ options[selectedIndex] }}</view>
                </picker>
              </view>
              <view><u-icon name="arrow-right"></u-icon></view>
            </view>
          </u-form-item>
          <view>
            上传图片
          </view>
          <u-form-item :border-bottom="false">
            <view style="margin-top: 18rpx;">
              <view class="pre-box">
                <view class="pre-item" v-for="(item, index) in lists" :key="index">
                  <image class="pre-item-image" :src="item.url" mode="scaleToFill"></image>
                  <view class="u-delete-icon" @tap.stop="deleteItem(index)">
                    <u-icon name="close" size="20" color="#ffffff"></u-icon>
                  </view>
                  <u-line-progress v-if="item.progress > 0 && !item.error" :show-percent="false" height="16"
                    class="u-progress" :percent="item.progress"></u-line-progress>
                </view>
                <u-upload :custom-btn="true" @on-choose-fail="onChooseFail" :before-remove="beforeRemove" ref="uUpload"
                  :show-upload-list="false" :action="action" :auto-upload="true" :file-list="fileList"
                  :show-progress="true" :deletable="true" :max-count="8" @on-list-change="onListChange" :size-type="['compressed']" :max-size="FileSize">
                  <view slot="addBtn">
                    <image src="/static/image/upload_icon.png" mode=""
                      style="width: 160rpx;height: 150rpx;border-radius: 12rpx;"></image>
                  </view>
                </u-upload>
              </view>
            </view>
          </u-form-item>
        </u-form>

        <view @tap="submit" class="comment">
          <text>退款</text>
        </view>
      </view>

    </u-popup>
    <view v-show="paypop == 1" @tap="gotopay" class="comment">
      <text>去支付</text>
    </view>



  </view>
</template>

<script>
import { getUserOrderInfo, cancelOrder, checkWxPayResult, orderReturn,toPayJhzs } from '@/api/common'
import { WeChat } from '@/api/weChat'
import { FileSize } from '@/common/fileSize';
let that = null;
export default {
  data() {
    return {
      FileSize,
      showorders: false,
      shoplist: [],
      addlist: [],
      paypop: 0,
      show: false,
      showchoose: false,
      form: {
        name: '',
        intro: '',
      },
      valuerradio: 'orange',
      list: [
        {
          name: '价格有点贵',
          disabled: false
        },
        {
          name: '买错了',
          disabled: false
        },
        {
          name: '收货地址选错',
          disabled: false
        },
        {
          name: '暂时不需要了',
          disabled: false
        },
        {
          name: '其他',
          disabled: false
        }
      ],
      rules: {
        name: [
          {
            required: true,
            min: 1,
            message: '请输入选择',
            // 可以单个或者同时写两个触发验证方式 
            trigger: ['change', 'blur'],
          }
        ],
      },
      options: ['未收到货', '与内容不符'],
      selectedIndex: 0,
      lists: [],
      action: 'http://127.0.0.1:7001/upload',
      fileList: [],
      orderNum: '',
      orderslist: [],
      cancelorders: '',
      timestamp: 0,
      createTime: 0,
      timeOut: 0,
      now: 0,
      selectedValue: '',
      Verification: ''
    };
  },
  onReady() {

  },
  onShow() {
    this.getorders()
  },
  onLoad(e) {
    console.log('看看订单号', e)
    this.orderNum = e.orderNum
    that = this;
    const eventChannel = that.getOpenerEventChannel();
    // 监听redactInfo事件，获取上一页面通过eventChannel传送到当前页面的数据
    eventChannel.on("redactInfo", function (data) {
      console.log("获取到信息", data.shoplist);
    });
  },


  methods: {
    openReason() {
      this.showchoose = true
    },
    paste(value){
      uni.setClipboardData({ data: value });
    },
    //订单详情
    getorders() {
      let obj = {
        orderNum: this.orderNum
      }
      getUserOrderInfo(obj).then(res => {
        this.orderslist = res.data
        console.log('看看时间', res.data.createTime)
        var second = res.data.createTime.substring(11)
        const [hours, minutes, seconds] = second.split(':').map(Number);
        this.createTime = hours * 3600 + minutes * 60 + seconds;
        console.log('看看秒看看秒', this.createTime)
        this.timeOut = this.createTime + 600
        console.log('看看结束秒看看结束秒', this.timeOut)
        const current_time = new Date()
        const date = new Date(current_time);
        const hourstwo = date.getHours(); // 获取小时
        const minutestwo = date.getMinutes(); // 获取分钟
        const secondstwo = date.getSeconds(); // 获取秒
        console.log(`${hourstwo}:${minutestwo}:${secondstwo}`);
        this.now = hourstwo * 3600 + minutestwo * 60 + secondstwo;
        console.log('看看当前秒看看当前秒', this.now)
        this.timestamp = this.timeOut - this.now
        console.log('看看时间差', this.timestamp)
        this.paypop = this.orderslist.orderStatus
        console.log(this.paypop);
        this.Verification = res.data.orderProductsList[0].qrUrl
        console.log('看看核销码', this.Verification)
      })
    },
    goback() {
      uni.navigateBack({
        delta: 1
      });
    },
    moneyback() {
      this.form.name = ''
      this.show = true
      console.log('看看看东西给', this.options[0])
    },
    onPickerChange(event) {

      this.selectedIndex = event.mp.detail.value;
      this.selectedValue = this.options[this.selectedIndex];
      // 处理选择器数值变化的逻辑
    },
    submit() {
      if (this.selectedValue == '') {
        this.$u.toast('请选择退款原因');

      } else {
        let obj = {
          orderNum: this.orderNum,
          refReason: this.selectedValue
        }
        orderReturn(obj).then(res => {
          console.log('退款', res)
          this.paypop = res.data.orderStatus
          this.show = false
        })

      }
    },
    bindPickerChange: function (e) {
      console.log('picker发送选择改变，携带值为', e.detail.value)
      this.index = e.detail.value
    },
    refund(data) {
      console.log(data)
      this.form.name = this.selectorObj[data].cateName
    },
    deleteItem(index) {
      console.log('1')
      this.$refs.uUpload.remove(index);
    },
    onChooseFail(e) {
      // console.log(e);
    },
    beforeRemove(index, lists) {
      return true;
    },
    onListChange(lists) {
      console.log('onListChange', lists);
      this.lists = lists;
    },
    radioChange(e) {
      console.log(e);
      this.cancelorders = e
    },
    radioGroupChange(e) {
      // console.log(e);
    },
    gotopay() {
      let obj = { 
          orderNum: this.orderNum,
          payType:2   //1微信2支付宝
        };
      toPayJhzs(obj).then(res=>{
        plus.runtime.openURL(res.data.qr_code); 
      })
      // this.checkorder()
      // this.getorders()
      // WeChat().then(response=>{
      //   console.log(this.orderslist.orderNum,this.orderslist.userId);
      //   if(response.code==200){
      //     console.log('是否有openId',response);
      //     plus.share.getServices(res => {
      //       let sweixin = res.find(i => i.id === 'weixin')
      //       if (sweixin.nativeClient) {
      //         // 分享跳转到微信小程序
      //           console.log('是否有openId',response);
      //           sweixin.launchMiniProgram({
      //           id: "gh_85a908b30cde", //  关联微信小程序的原始ID（"gh_"开头的字符串）   
      //           type: 2, //   微信小程序版本类型，可取值： 0-正式版； 1-测试版； 2-体验版。 默认值为0。
      //           path: '/pages/index/index?flag='+response.data+'&orderNum='+this.orderslist.orderNum+'&userId='+this.orderslist.userId, 
      //         }, res => {
      //           //  this.checkorder()
      //           console.log('成功唤起微信小程序');
      //         }, err => {
      //           console.log('失败');
      //           uni.showToast({
      //             title: '唤起小程序失败',
      //             icon: 'error',
      //           })
      //         })
      //         } else {
      //           // 没有获取到微信分享服务
      //           uni.showToast({
      //             title: '该手机没有微信',
      //             icon: 'error',
      //           })
      //         }
      //       }, err => {
      //         // 获取分享服务列表失败
      //     })
      //   }
      // })
    },
    //取消订单
    setcancelorder() {
      let obj = {
        orderNum: this.orderNum,
        cancelReason: this.cancelorders
      }
      cancelOrder(obj).then(res => {
        console.log('取消订单', res)
        this.cancelorders = res.data.cancelReason
        this.paypop = res.data.orderStatus
        this.showorders = false
      })
    },
    //手动查询订单状态
    checkorder() {
      let obj = {
        orderNum: this.orderNum
      }
      checkWxPayResult(obj).then(res => {
        console.log('订单状态详情', res)

      })
    },
  }
};
</script>

<style scoped lang="scss">
.content {
  min-height: 100vh;
  width: 100vw;
  background-color: #E6E6E6;
  display: flex;
  flex-direction: column;
  padding: 0 32rpx 74rpx 32rpx;

  .topbox {
    display: flex;
    justify-content: space-between;
    margin-top: 114rpx;

    .topRight {
      display: flex;
      justify-content: space-between;
      width: 168rpx;

      image {
        width: 40rpx;
        height: 40rpx;
      }

      .moreimg {
        margin-top: 12rpx;
        width: 36rpx;
        height: 12rpx;
      }
    }
  }

  .payon {
    display: flex;
    align-items: center;
    margin-top: 48rpx;
    margin-left: 24rpx;
    height: 68rpx;
    font-family: PingFang SC, PingFang SC;
    font-weight: bold;
    font-size: 48rpx;
    color: #000000;
    justify-content: space-between;

    image {
      margin-right: 24rpx;
      width: 48rpx;
      height: 48rpx;
    }
  }

  .payontwo {
    display: flex;
    align-items: center;
    margin-top: 48rpx;
    margin-left: 24rpx;
    height: 68rpx;
    font-family: PingFang SC, PingFang SC;
    font-weight: bold;
    font-size: 48rpx;
    color: #000000;

    image {
      margin-right: 24rpx;
      width: 64rpx;
      height: 48rpx;
    }
  }

  .ordersmain {
    border-radius: 12rpx 12rpx 12rpx 12rpx;
    padding: 32rpx 22rpx;
    background-color: white;
    margin: 18rpx 0;
    font-family: PingFang SC, PingFang SC;
    font-weight: 400;
    font-size: 32rpx;
    color: #000000;


    .ordermainone {
      display: flex;
      justify-content: space-between;

      .orderleft {
        font-family: PingFang SC, PingFang SC;
        font-weight: 400;
        font-size: 32rpx;
        color: #000000;

        font-style: normal;
      }

      .orderright {
        font-family: PingFang SC, PingFang SC;
        font-weight: 400;
        font-size: 28rpx;
        color: #d12c25;

        font-style: normal;
      }
    }

    .ordermaintwo {
      display: flex;
      margin-top: 18rpx;

      image {
        width: 156rpx;
        height: 132rpx;
      }

      .ordertwoname {
        padding: 0 0 38rpx 24rpx;
        font-family: PingFang SC, PingFang SC;
        font-weight: 400;
        font-size: 32rpx;
        color: #000000;
        line-height: 30rpx;
        font-style: normal;
      }

      .ordertwonum {
        display: flex;
        justify-content: space-between;
        padding-left: 24rpx;
        align-items: center;

        .ordertwotop {
          font-family: PingFang SC, PingFang SC;
          font-weight: 400;
          font-size: 26rpx;
          color: #999999;
        }

        .ordertwobottom {
          font-family: PingFang SC, PingFang SC;
          font-weight: bold;
          font-size: 28rpx;
          color: #ff0000;
        }

        .orderbottomtwo {
          font-family: PingFang SC, PingFang SC;
          font-weight: bold;
          font-size: 40rpx;
          color: #ff0000;
        }
      }
    }

    .Distribution {
      margin-top: 18rpx;
      font-family: PingFang SC, PingFang SC;
      font-weight: 400;
      font-size: 32rpx;
      color: #999999;

      font-style: normal;
    }

    .pay {
      margin-top: 18rpx;
      display: flex;
      justify-content: space-between;
      font-family: PingFang SC, PingFang SC;
      font-weight: 400;
      font-size: 32rpx;
      color: #000000;

      font-style: normal;

      image {
        width: 36rpx;
        height: 36rpx;
        margin-right: 14rpx;
      }
    }

  }

  .ordersnum {
    padding: 32rpx 24rpx;
    background-color: white;
    font-family: PingFang SC, PingFang SC;
    font-weight: 400;
    font-size: 28rpx;
    color: #000000;
    border-radius: 12rpx;

    .ordersone {
      display: flex;
      justify-content: space-between;
      align-items: center;
      margin-top: 24rpx;
    }
  }

  .radio {
    font-family: PingFang SC, PingFang SC;
    font-weight: 400;
    font-size: 28rpx;
    color: #999999;
    margin: 24rpx 0;
  }

  .radiobutton {
    font-family: PingFang SC, PingFang SC;
    font-weight: 400;
    font-size: 28rpx;
    color: #999999;
    margin-bottom: 72rpx;
    display: flex;
    // justify-content: space-between;
  }

  .QrCode {
    border-radius: 12rpx;
    background-color: #fff;
    margin-top: 18rpx;
    padding: 34rpx 24rpx;

    .QrCodeTop {
      display: flex;
      justify-content: space-between;

      .Green {
        color: #16D356;
      }

      .grey {
        color: #999999;
      }
    }

    .QrImg {
      display: flex;
      justify-content: center;
      margin-top: 18rpx;

      image {
        width: 344rpx;
        height: 344rpx;
      }
    }

    .QrgreyImg {
      display: flex;
      justify-content: center;
      margin-top: 18rpx;

      image {
        width: 344rpx;
        height: 344rpx;
        opacity: 0.45;
      }
    }

    .QrCodeM {
      display: flex;
      justify-content: center;
      align-items: center;
      margin-top: 18rpx;

      view {
        margin-left: 20rpx;
        margin-top: -10rpx;
        color: #D12C25;
      }
    }

    .QrText {
      text-align: center;
      font-size: 28rpx;

      view {
        margin-top: 10rpx;
      }
    }
  }

  .comment {
    position: fixed;
    bottom: 58rpx;
    border-radius: 1998rpx;
    width: 90%;
    height: 96rpx;
    line-height: 96rpx;
    text-align: center;
    background-color: #D12C25;
    color: white;
  }

  .commentback {

    position: fixed;
    bottom: 58rpx;
    border-radius: 1998rpx;
    padding: 26rpx 280rpx;
    text-align: center;
    background-color: #D12C25;
    color: white;
  }

  .gotoradio {
    text-align: center;
    padding: 26rpx 310rpx;
    background-color: #D12C25;
    color: white;
    border-radius: 1998rpx;
    position: fixed;
    bottom: 64rpx;
    left: 32rpx;
    font-weight: bold;
    font-size: 32rpx;
  }
}
</style>